<template>
	<view>
		<u-cell-group style="margin-top: 25rpx;">
			<view class="list-cell u-border-bottom">
				<u-image width="128rpx" height="128rpx" :src="brand.logo" :lazy-load="true"></u-image>
				<text class="anchor-text-top"><h2>{{brand.name}}</h2></text>
			</view>
		</u-cell-group>
		
		<u-collapse>
			<u-collapse-item v-for="(item,i) in vcList" :key="i" :title="item.name" :open="i==0">
				<u-cell-group>
					<view class="list-cell u-border-bottom" @tap="showPopupSeries(s)" v-for="(s,idx) in item.list">
						<text class="anchor-text">{{s.name}}</text>
					</view>
				</u-cell-group>
			</u-collapse-item>
		</u-collapse>

		<u-popup v-model="showSeries" mode="right" :closeable="true" width="70%">
			<u-cell-group :title="currentSeries.name" style="">
				<u-collapse>
					<u-collapse-item v-for="(item,i) in models" :key="i" :title="item.yeartype" :open="i==0">
						<u-cell-group>
							<view class="list-cell u-border-bottom" @tap="showModel(m)" v-for="(m,i2) in item.list" :key="i2">
								<text class="anchor-text">{{m.name}}</text>
							</view>
						</u-cell-group>
					</u-collapse-item>
				</u-collapse>
			</u-cell-group>

		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabs: [{
					name: '在售'
				}, {
					name: '未售/停售'
				}],
				tabsCategory: [],
				current: 0,
				cateCurrent: 0,
				dropdownYear:[],
				yearCurrent:0,
				vcList: [],
				brand: {},
				ajaxUrl: '',
				seriesState: '0',
				levelCategory: '',
				showSeries: false,
				models: [],
				currentSeries: {},
				modelYear:'年款'


			}
		},
		
		created() {
			this.brand = uni.getStorageSync('brandInfo');
			// uni.removeStorageSync('brandInfo')
			this.getSeries()
		},
		
		methods: {
			tabChange(index) {
				this.current = index
				if (index == 0) {
					this.seriesState = 0 //在售
				} else if (index == 1) {
					this.seriesState = 1 //停售
				}
				this.getSeries()

			},
			tabCageChange(index) {
				if (index == 0) {
					this.levelCategory = ''
				} else {
					this.cateCurrent = index
					this.levelCategory = this.tabsCategory[index].name
				}

				this.getSeries()

			},
			
			getLevelCategory() {
				this.tabsCategory = []
				let that = this
				this.$u.get(`/api/car/leveCategory/${this.brand.bid}`, {}, {}).then(res => {
					res.forEach(item => {
						this.tabsCategory.push({
							name: item
						})
					})
				}).catch((e) => {
					console.log(e)
				})
			},
			
			getSeries() {
				let that = this
				if(that.brand == null)
					return
				
				uni.showLoading({
					title: "加载中..."
				})
				uni.request({
					url: getApp().globalData.serviceUrl + "/chexk/kuybChexk/getSeriesList",
					data: {
						brandId: that.brand.id
					},
					header: {
					},
					method: 'GET',
					success: (resp) => {
						if (resp.data.success) {
							that.vcList = resp.data.result
						}
					},
					complete() {
						uni.hideLoading()
					}
				});
				
			},
			
			showPopupSeries(v) {
				this.currentSeries = v
				this.models = []
				let that = this
				
				uni.showLoading({
					title: "加载中..."
				})
				uni.request({
					url: getApp().globalData.serviceUrl + "/chexk/kuybChexk/getModelList",
					data: {
						seriesId: v.id
					},
					header: {
					},
					method: 'GET',
					success: (resp) => {
						uni.hideLoading()
						if (resp.data.success) {
							that.models = resp.data.result
							that.showSeries = true
							
						} else {
							uni.showToast({
								icon:'none',
								title:resp.data.message
							})
						}
					},
					complete() {
						
					}
				});
			},
			
			showModel(m) {
				if(m == null) {
					uni.showToast({
						title: "m == null",
						icon: 'none'
					})
					
					return
				}
				
				try {
					m.carid = m.id
					let tmp = this.brand.name + " " + this.currentSeries.name + " " + m.name
					m.fullName = tmp
					m.shifjk = this.currentSeries.shifjk
					m.pinp = this.brand.id
					
					debugger
					
				} catch(error) {
					uni.showToast({
						title: error,
						icon: 'none'
					})
				}
				
				uni.setStorage({
					key:'carInfo',
					data:m,
					success() {
						uni.redirectTo({
							url:"../baofShis/baofShis",
							fail (error) {
								console.log(error)
							}
						})
					}
				})
			}
		}

	}
</script>

<style lang="scss" scoped>
	.list-cell {
		display: flex;
		box-sizing: border-box;
		width: 100%;
		padding: 10px 24rpx;
		overflow: hidden;
		color: #323233;
		font-size: 14px;
		line-height: 24px;
		background-color: #fff;
	}

	.anchor-text {
		margin-left: 20rpx;
		margin-top: 10rpx;
	}
	
	.anchor-text-top {
		margin-left: 30rpx;
		margin-top: 40rpx;
	}
</style>
